package com.ruoyi.auth.aspect;

import com.ruoyi.common.core.domain.R;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: check-token拦截器
 * 1. 统一返回token格式
 * @author: liuxing
 */
@Slf4j
@Component
@Aspect
public class CheckTokenAspect {

    @Pointcut("execution(* org.springframework.security.oauth2.provider.endpoint.CheckTokenEndpoint.checkToken(..))")
    public void pointcut() {
    }

    @Before("pointcut()")
    public void handleBeforeMethod() {
    }

    @Around("pointcut()")
    public Object handleControllerMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = joinPoint.proceed();
        Map responseEntity = (Map) proceed;
//        Map<String, ?> body = responseEntity.getBody();
        R rt = R.ok(responseEntity);
        Map newMap = new HashMap();
        newMap.put("code", rt.getCode());
        newMap.put("msg", rt.getMsg());
        newMap.put("data", rt.getData());
        return newMap;
    }
}
